<?php
if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' );
/**
*
* @version $Id$
* @package VirtueMart
* @subpackage html
* @copyright Copyright (C) 2007 Kristian Arndt. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
* VirtueMart is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See /administrator/components/com_virtuemart/COPYRIGHT.php for copyright notices and details.
*
* http://virtuemart.net
*/
mm_showMyFileName( __FILE__ );

/* INIT */
require_once(CLASSPATH . 'ps_wishlist.php' );

$Itemid = JRequest::getInt('Itemid');

// LOGGED-IN ?
if (!isset( $GLOBALS['my'] ) || !isset( $GLOBALS['my']->id ) || $GLOBALS['my']->id <= 0)
{
	show_login( $Itemid );
}
else
{
	// OPEN form
	echo '<h1 class="componentheading">Save Cart as Wishlist</h1>';
	echo '<form action="' . $mm_action_url . 'index.php" method="post" name="addtowishlist" id="addtowishlist">';

	show_wishlists( $GLOBALS['my']->id );
	show_new_wishlist_box();

	// HIDDEN variables
	echo '<input type="hidden" name="page" value="wishlist.index" />';
	echo '<input type="hidden" name="func" value="wishlistSaveCart" />';
	echo '<input type="hidden" name="option" value="' . $GLOBALS['option'] . '" />';
	echo '<input type="hidden" name="Itemid" value="' . $Itemid . '" />';

	// CLOSE form
	echo '<br style="clear:both;" /><input type="submit" name="submit" value="' . $GLOBALS['VM_LANG']->_('PHPSHOP_WISHLIST_SAVE') . '" class="button" />';
	echo '</form><br style="clear:both;" />';
}


/* --------------- */

function show_login( $Itemid )
{
	global $VM_LANG, $mosConfig_allowUserRegistration, $mm_action_url;

	echo '<div class="wishlist-login" style="border:1px solid #999999;margin-top:15px;padding:6px;">';
	echo '<h4>Please Login to Create Wishlist</h4>';

	$_SERVER['QUERY_STRING'] .= '&option=' . $GLOBALS['option'];
	$_SERVER['QUERY_STRING'] .= '&Itemid=' . $Itemid;
	$_SERVER['QUERY_STRING'] .= '&page=wishlist.save_cart';

	include( PAGEPATH . 'checkout.login_form.php' );
	echo "or <a href=\"".JRoute::_('index.php?option=com_virtuemart&amp;page=shop.registration', true, 0)."\" title=\"Register\">Create an Account to Get Started</a>";
	echo '</div>';
}

/**
 * show the wishlists dropdown, if wishlists exist for this user
 * @param int $user_id
 */
function show_wishlists( $user_id )
{
	// SELECT wishlist
	$wl_list = ps_wishlist::get_wishlists( $user_id );
	if (count( $wl_list ) > 0)
	{
		echo "<p>";
		echo $GLOBALS['VM_LANG']->_('PHPSHOP_WISHLIST_NAME_SELECT');
		echo ' <select name="wishlist_id">';
		foreach ($wl_list as $wl) 
		{
			echo '<option value="' . $wl['wishlist_id'] . '">' . $wl['name'] . '</option>';
		}
		echo '</select>';
		echo "</p>";

		echo '<p>';
		echo $GLOBALS['VM_LANG']->_('PHPSHOP_WISHLIST_OR');
		echo '</p>';
	} 
	else
	{
//		echo $VM_LANG->_('PHPSHOP_WISHLIST_NO_WISHLISTS');
	}
}

/**
 * Show the textbox to enter a new wishlist name.
 */
function show_new_wishlist_box()
{
	echo "<p>";
	echo $GLOBALS['VM_LANG']->_('PHPSHOP_WISHLIST_NAME_NEW');
	echo ' <input type="text" class="inputbox" name="wishlist_name" value="" />';
	echo "</p>";
}
?>
